package ClientComm;

import UserInterface.Client_View;
import Threads.QueueClient;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Timer;
/**
 *
 * Hybrid Controller for CREIPER Client
 * 
 * @author Travis Shuff, Dakota Brown
 * @since 9/30/11
 * 
 */
public class Client {
    public int robotCount;
    public ArrayList<Robot> robotList;
    
    private Client_View view;
    public static ConnectionObject connObj;
    public QueueClient queueClient;
    private  Timer timer;
    private String queuePos = "CRIEPER - Queue: ";
    public Client(Client_View view_In) {
        this.view = view_In;
        
        
        
          timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setQueuePosition();
                view.validate();
            }
        });
        
    }
    
    /**
     * Sets queue position value in main window title bar
     * 
     * @param pos current position in queue
     * @since 10/6/11
     */
    public void setQueuePosition() {
        queuePos = QueueClient.queuePos;
        view.setTitle(queuePos);
        
        
    }
    
    public void connect() {
        if (connObj != null) {
            view.displayStatus(connObj.connectToDatabase());
            if (!QueueClient.run) {
                queueClient = new QueueClient(connObj, queuePos);
                new Thread(queueClient).start();
                timer.start();
            }
            robotCount = connObj.getRobotsFromDB();
            robotList = connObj.getRobotList();
            
        } else {
            view.displayStatus("Check Connection Settings");
        }
    }
}
